# delimit ;
clear ;
cd "replication" ;
set more off ; 

* *************************************************************************** ;
* load data 
* *************************************************************************** ;

use "./data/endline.dta", clear ; 

****************************************************************************** ;
* generate randomization strata and wave-class variable
****************************************************************************** ;

egen strata = group(wave neighborhood female mfi) ;
egen waveclass = group(wave class) ;

***************************************************************************** ;
* build table of summary statistics
***************************************************************************** ;
gen varname = "" ;
gen mean_hee = "" ; 
gen mean_heec = "" ;
gen mean_control = "" ;
gen pval = "" ; 
gen stars = "" ;

label var varname "" ;
label var mean_hee "HEE" ;
label var mean_heec "HEEC" ;
label var mean_control "\shortstack[r]{Financial\\Films}" ;
label var pval "\shortstack[r]{\(F\)-test \\ \(p\)-value}" ; 

****************************************************************************** ;
* get summary statistics
****************************************************************************** ;

local i = 1 ;

pause on ;

foreach var of varlist ever_had_ins bought_ins { ;
	di "`var'" ;
	local vartext: variable label `var' ;
	replace varname = "`vartext'" in `i' ; 
	
	qui summ `var' if healthonly == 1 ;
	qui replace mean_hee = string(r(mean), "%9.3f") in `i' ;

	qui summ `var' if healthandpay == 1 ;
	qui replace mean_heec = string(r(mean), "%9.3f") in `i' ;

	qui summ `var' if control == 1 ;
	qui replace mean_control = string(r(mean), "%9.3f") in `i' ;
	
	areg `var' healthonly healthandpay, a(strata) robust ;
	test healthonly healthandpay ;
	qui replace pval = string(r(p), "%9.3f") in `i' ;
	qui replace pval = "" if pval == "." ;
	local ++i ;

} ;

****************************************************************************** ;
* output results 
****************************************************************************** ;

keep varname mean* pval stars  ;
order varname mean_hee mean_heec mean_control pval stars;
dropmiss, obs force ;

replace stars = "*" if real(pval) < 0.1 ;
replace stars = "**" if real(pval) < 0.05 ;
replace stars = "***" if real(pval) < 0.01 ;

texsave using "./output/appendix-table-insurance.tex",
	replace frag nofix
	title("Health Insurance Take-up Rates") 
	marker(appendix-table-insurance)
	varlabels
	location(h)
	align(lrrrr@{}l)
	width(0.88\linewidth) 
	footnote("\textit{Notes:} 
		This table reports, by study arm, the proportion of households with health insurance. 
		Data from the endline survey.
		The last column reports the \(p\)-value of the \(F\)-test for the joint 
		significance of the coefficients \(\beta_1\) and \(\beta_2\) in the main specification of the paper, Equation \ref{reg-equation}.") ; 
		
filefilter "./output/appendix-table-insurance.tex" "./output/appendix-table-insurance-v00.tex", from("\BSend{tabularx}") to("") replace ;
filefilter "./output/appendix-table-insurance-v00.tex" "./output/appendix-table-insurance-v01.tex", from("\BSbottomrule \BSaddlinespace[\BSbelowrulesep]") to("\BSbottomrule \BSend{tabularx}") replace ;
filefilter "./output/appendix-table-insurance-v01.tex" "./output/appendix-table-insurance-v02.tex", from("\BSbegin{flushleft}") to("\BScaptionsetup{width=0.88\BSlinewidth} \BScaption*{") replace ;
filefilter "./output/appendix-table-insurance-v02.tex" "./output/appendix-table-insurance-v03.tex", from("\BSend{flushleft}") to("}") replace ;
filefilter "./output/appendix-table-insurance-v03.tex" "./output/appendix-table-insurance.tex", from("\BStoprule") to("\BStoprule & \BSmulticolumn{3}{c}{Study Arm} & \BStabularnewline \BScmidrule{2-4}") replace ;

forvalues i = 0/3 { ;
	erase "./output/appendix-table-insurance-v0`i'.tex" ;
} ;

exit ;


